/** ***********************************************
 * CodeGen
 *
 * @author  Jonnie Spratley
 * @date 3/05/2009
 * @licence MCIYC
 * @website http://jonniespratley.com/code
 * @contact jonniespratley@gmail.com
 *********************************************** */
package com.jonniespratley.codegen.services
{
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;

	[Bindable]
	public class RequestVO
	{
		public var requestSO:SharedObject;
		public var serviceType:String;
		public var methodType:String;
		public var requestURL:String;

		public var requestDate:Date = new Date();

		//Sent Time and Size
		public var requestSentTime:Number;
		public var requestSentSize:Number;

		//Recieved Time and Size
		public var requestRecievedTime:Number;
		public var requestRecievedSize:Number;
		public var requestParseTime:Number;
		public var requestTotalTime:Number;
		public var requestRecievedRaw:String;
		public var requestRecievedCollection:ArrayCollection = new ArrayCollection();
		public var requestHistoryCollection:ArrayCollection;

		public function  RequestVO(){}

		public function newRequest(
									serviceType:String,
									methodType:String,
									requestURL:String,
									requestSentTime:Number,
									requestSentSize:Number,
									requestRecievedTime:Number,
									requestRecievedSize:Number,
									requestParseTime:Number,
									requestTotalTime:Number,
									requestRecievedCollection:ArrayCollection,
									requestRecievedRaw:String
									):void
		{

			this.serviceType = serviceType;
			this.methodType = methodType;
			this.requestURL = requestURL;

			//Sent time and size
			this.requestSentTime = requestSentTime;
			this.requestSentSize = requestSentSize;

			//Recieved time and size
			this.requestRecievedTime = requestRecievedTime;
			this.requestRecievedSize = requestRecievedSize;

			this.requestParseTime = requestParseTime;
			this.requestTotalTime = requestTotalTime;

			this.requestRecievedCollection = requestRecievedCollection;
			this.requestRecievedRaw = requestRecievedRaw;
		}

		public function addRequest( request:RequestVO ):void
		{
			if ( requestHistoryCollection ){
				this.requestHistoryCollection.addItem( request );
				saveHistory();
			} else {
				requestHistoryCollection = new ArrayCollection();
				this.requestHistoryCollection.addItem( request );
				saveHistory();
			}
		}

		public function getHistory():void
		{
			requestSO = SharedObject.getLocal( 'cgHistory' );

			if ( requestSO.data.cgHistory != null )
			{
				this.requestHistoryCollection = requestSO.data.cgHistory as ArrayCollection;
			} else {
				trace( 'No History Saved' );
			}
		}

		public function saveHistory():void
		{
			requestSO = SharedObject.getLocal( 'cgHistory' );
			requestSO.data.cgHistory = this.requestHistoryCollection;
		}
	}
}